package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
@SuppressWarnings("all")
public class OrderAction extends BaseAction<Order> {

	// 获取表单传递过来的省市区信息
	private String recAreaInfo;
	private String sendAreaInfo;

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	@Action(value = "order_add", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String add() {
		// 手动封装area关联,要与订单类里面的属性保持一致
		Area sendArea = new Area();
		String[] sendAreaData = sendAreaInfo.split("/");
		// 把省市区分别进行封装
		sendArea.setProvince(sendAreaData[0]);
		sendArea.setCity(sendAreaData[1]);
		sendArea.setDistrict(sendAreaData[2]);

		// 封装收获地区地址
		Area recArea = new Area();
		String[] recAreaData = recAreaInfo.split("/");
		// 把省市区分别进行封装
		recArea.setProvince(recAreaData[0]);
		recArea.setCity(recAreaData[1]);
		recArea.setDistrict(recAreaData[2]);

		model.setRecArea(recArea);
		model.setSendArea(sendArea);

		// 获取登陆时存在session里面值
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());

		// 调用webService服务,将数据传递到bos_management
		WebClient.create("http://localhost:8080/bos_management/service/orderService/orderAdd")
				.type(MediaType.APPLICATION_JSON).post(model);

		return SUCCESS;
	}

}
